package org.example.controller;

import entity.Article;
import org.example.fegin.ArticleFeginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import util.ResponseData;
import vo.ArticleVo;

import java.util.List;

/**
 * @author : xuzhennan
 * @Project : 787k-cloud
 * @Package : org.example.controller
 * @email : xuzhennan@58.com
 * @date : 2021年08月29日 下午2:26
 */
@RestController
@RequestMapping("/view")
public class ArticleViewController {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 利用restTemplate 这个方法调用的弊端就是必须要写正确的url 「http://localhost:9000」
     *
     * @param pageNumber
     * @param pageSize
     * @return http://localhost:8000/view/findArticleByTemplate?pageNumber=1&pageSize=2
     */
    @RequestMapping("/findArticleByTemplate")
    public ResponseData findArticleByTemplate(@RequestParam("pageNumber") int pageNumber, @RequestParam("pageSize") int pageSize) {
        ArticleVo articleVo = restTemplate.getForObject("http://localhost:9000/article/findArticlesWithPage?pageNumber=" + pageNumber + "&pageSize=" + pageSize, ArticleVo.class);
        return ResponseData.buildSuccess(articleVo);
    }

    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     *
     * discoveryClient配合ribbon 实现负载均衡
     * public class ControllerApplication {
     * <p>
     * public static void main(String[] args) {
     * SpringApplication.run(ControllerApplication.class, args);
     * }
     *
     * @param pageNumber
     * @param pageSize
     * @return http://localhost:8000/view/findArticleByDiscoveryClient?pageNumber=1&pageSize=2
     * @Bean public RestTemplate getRestTemplate(){
     * return new RestTemplate();
     * }
     * }
     * 在ControllerApplication 的RestTemplate这里加入注解@LoadBalanced
     */
    @RequestMapping("/findArticleByDiscoveryClient")
    public ResponseData findArticleByDiscoveryClient(@RequestParam("pageNumber") int pageNumber, @RequestParam("pageSize") int pageSize) {
        List<ServiceInstance> instances = discoveryClient.getInstances("787k-cloud-search");
        ServiceInstance serviceInstance = instances.get(0);
        ArticleVo articleVo = restTemplate.getForObject("http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/article/findArticlesWithPage?pageNumber=" + pageNumber + "&pageSize=" + pageSize, ArticleVo.class);
        System.out.println("host is:" + serviceInstance.getHost() + ",port is:" + serviceInstance.getPort());
        return ResponseData.buildSuccess(articleVo);
    }

    /**
     * 通过ribbon实现负载均衡 如果开启ribbon 上边两个链接都不能使用
     * 如果想要测试上边两个链接，需要关闭ribbon
     * public class ControllerApplication {
     * <p>
     * public static void main(String[] args) {
     * SpringApplication.run(ControllerApplication.class, args);
     * }
     *
     * @param pageNumber
     * @param pageSize
     * @return
     * @Bean 这里加了@LoadBalanced注解
     * @LoadBalanced public RestTemplate getRestTemplate(){
     * return new RestTemplate();
     * }
     * }
     */
    @RequestMapping("/findArticleByRibbon")
    public ResponseData findArticleByRibbon(@RequestParam("pageNumber") int pageNumber, @RequestParam("pageSize") int pageSize) {
        ArticleVo articleVo = restTemplate.getForObject("http://787k-cloud-search/article/findArticlesWithPage?pageNumber=" + pageNumber + "&pageSize=" + pageSize, ArticleVo.class);
        return ResponseData.buildSuccess(articleVo);
    }

    @Autowired
    private ArticleFeginService articleFeginService;

    /**
     * @param pageNumber
     * @param pageSize
     * @return http://localhost:8000/view/findArticles?pageNumber=1&pageSize=2
     */
    @RequestMapping("/findArticles")
    public ResponseData findArticles(@RequestParam("pageNumber") int pageNumber, @RequestParam("pageSize") int pageSize) {
        ArticleVo articles = articleFeginService.findArticles(pageNumber, pageSize);
        return ResponseData.buildSuccess(articles);
    }

    /**
     * http://localhost:8000/view/articleDetail?id=1
     * @param id
     * @return
     */
    @RequestMapping("/articleDetail")
    public ResponseData articleDetail(@RequestParam("id") int id) {
        Article article = articleFeginService.getArticleDetail(id);
        return ResponseData.buildSuccess(article);
    }
}
